package com.metaera.app.ui.activities;

import android.graphics.Color;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;

import com.luck.picture.lib.entity.LocalMedia;

import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.UserEditPresenter;
import com.metaera.app.controller.presenter.UserInfoPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;

import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.event.UserEditEvent;
import com.metaera.app.utils.image.GlideApp;
import com.metaera.app.utils.image.GlideEngine;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

public class UserEditCenterActivity extends BaseActivity {

    private ImageView ivAvatar;
    private ProgressBar progressBar;

    private TextView tvNickname, tvSex, tvEditTitle, tvIntroduce, tvPhoneNumber, tvWeibo,
            tvKnowledge, tvWechat, tvOfficialAccounts, tvTwitter, tvEmail;

    private UserDetailsResponse mUser;
    private UserEditPresenter   mPresenter;

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_user_edit_center;
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        // find
        ivAvatar = findViewById(R.id.iv_avatar);
        tvNickname = findViewById(R.id.tv_nickname);
        tvSex = findViewById(R.id.tv_sex);
        tvEditTitle = findViewById(R.id.tv_edit_title);
        tvIntroduce = findViewById(R.id.tv_introduce);
        tvPhoneNumber = findViewById(R.id.tv_phone_number);
        tvWeibo = findViewById(R.id.tv_weibo);
        tvKnowledge = findViewById(R.id.tv_knowledge);
        tvWechat = findViewById(R.id.tv_wechat);
        tvOfficialAccounts = findViewById(R.id.tv_official_accounts);
        tvTwitter = findViewById(R.id.tv_twitter);
        tvEmail = findViewById(R.id.tv_email);
         progressBar = findViewById(R.id.progress_bar);

        // init view
        ActivityTitleHelper.setTitle(this, R.string.edit_user_info);
        mUser = LocalConfigStore.getInstance().getUser();
        renderUserInfo();

        // network
        mPresenter = new UserEditPresenter();
        mPresenter.setModelAndView(new BasicNetworkRequestView<>(){

            @Override
            public void loading() {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void complete() {
                progressBar.setVisibility(View.GONE);
            }

        });

        // listener
        ivAvatar.setOnClickListener(v -> pictureSelector(ivAvatar));
        setClickListener(findViewById(R.id.layout_nickname), UserEditItemActivity.NICKNAME_TYPE, tvNickname);
        setClickListener(findViewById(R.id.layout_phone_number), UserEditItemActivity.PHONE_TYPE, tvPhoneNumber);
        setClickListener(findViewById(R.id.layout_wechat), UserEditItemActivity.WECHAT_TYPE, tvWechat);
        setClickListener(findViewById(R.id.layout_edit_title), UserEditItemActivity.TITLE_TYPE, tvEditTitle);
        setClickListener(findViewById(R.id.layout_introduce), UserEditItemActivity.INTRODUCE_TYPE, tvIntroduce);
        setClickListener(findViewById(R.id.layout_weibo), UserEditItemActivity.WEIBO_TYPE, tvWeibo);
        setClickListener(findViewById(R.id.layout_knowledge), UserEditItemActivity.KNOWLEDGE_TYPE, tvKnowledge);
        setClickListener(findViewById(R.id.layout_official_accounts), UserEditItemActivity.OFFICIAL_ACCOUNT_TYPE, tvOfficialAccounts);
        setClickListener(findViewById(R.id.layout_twitter), UserEditItemActivity.TWITTER_TYPE, tvTwitter);
        setClickListener(findViewById(R.id.layout_email), UserEditItemActivity.EMAIL_TYPE, tvEmail);

        findViewById(R.id.layout_sex).setOnClickListener(v -> startActivity(
                        UserEditItemActivity.newInstance(
                                this,
                                UserEditItemActivity.SEX_TYPE,
                                String.valueOf(mUser.getSex()),
                                mUser.getIs_show_wechat(),
                                mUser.getIs_show_phone(),
                                mUser.getSex()
                        )
                )
        );
    }

    private void renderUserInfo() {
        if (TextUtils.isEmpty(tvNickname.getText())){
            GlideApp.with(this).load(mUser.getAvatar_img()).avatar().into(ivAvatar);
        }

        tvNickname.setText(mUser.getNickname());
        setText(mUser.getPosition(), tvEditTitle);
        setText(mUser.getDesc(), tvIntroduce);
        setText(mUser.getMobile(), tvPhoneNumber);
        setText(mUser.getWeibo(), tvWeibo);
        setText(mUser.getKnowledge_planet(), tvKnowledge);
        setText(mUser.getWechat(), tvWechat);
        setText(mUser.getOfficial_account(), tvOfficialAccounts);
        setText(mUser.getTwitter(), tvTwitter);
        setText(mUser.getEmail(), tvEmail);
        setText(getString(mUser.getSex() == 1 ? R.string.u_edit_man : R.string.u_edit_woman), tvSex);
    }

    private void setClickListener(View view, int type, TextView textView) {
        view.setOnClickListener(v -> startActivity(
                        UserEditItemActivity.newInstance(
                                this,
                                type,
                                textView.getText(),
                                mUser.getIs_show_wechat(),
                                mUser.getIs_show_phone(),
                                mUser.getSex()
                        )
                )
        );
    }

    private void setText(String content, TextView textView) {
        if (!TextUtils.isEmpty(content)) {
            textView.setText(content);
            textView.setTextColor(Color.BLACK);
        }
    }

    private void pictureSelector(ImageView ivAvatar) {
        PictureSelector.create(this)
                .openGallery(SelectMimeType.ofImage())
                .isDisplayCamera(false)
                .setImageEngine(GlideEngine.createGlideEngine())

                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        String realPath = result.get(0).getRealPath();
                        mPresenter.updateAvatar(realPath, ivAvatar.getContext());
                        GlideApp.with(ivAvatar).load(realPath).avatar().into(ivAvatar);
                    }

                    @Override
                    public void onCancel() {

                    }

                });
    }

    @Subscribe
    public void event(UserEditEvent event) {
        UserInfoPresenter presenter = new UserInfoPresenter();
        presenter.setModelAndView(data -> {
            mUser = data;
            renderUserInfo();
        });
        presenter.execute();
    }

}
